﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineCooperation.DB;
using OnlineCooperation.Entity;
using System.Text;
using System.Net.Mail;

namespace OnlineCooperation
{
    public partial class TeamMember : System.Web.UI.Page
    {
        protected List<User> ulist;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["TeamID"] == null || Session["TeamID"].ToString() == "")
            {
                Response.Write("<script>alert('请先选择一个团队');window.location='TeamIndex.aspx';</script>");
            }
            RelUserTeamDB reld=new  RelUserTeamDB();
            TeamDB td=new TeamDB();
            Team t=td.GetTeamByID(int.Parse(Session["TeamID"].ToString()));
            ulist = reld.GetAllMember(t);

            if (IsPostBack)
            {
                string a1=Request.Form["__EVENTTARGET"];
                if (a1 == "viewteam_click")
                {
                    string a2 = Request.Form["__EVENTARGUMENT"];
                   
                    Response.Redirect("TeamPage.aspx");
                }
                else if (a1 == "createteam_click")
                {
                    Response.Redirect("SetupTeam.aspx");
                }
            }
        }

        protected void ButtonAddMember_Click(object sender, EventArgs e)
        {
            UserDB ud = new UserDB();
            String CurrentEmail = NewMemberEmail.Value;
            if (CurrentEmail == "")
            {
                Response.Write("<script>alert('请先输入ta的邮箱哦~')</script>");
                return;
            }
            String WantToSay = TeamDescription.Value;
            //将该用户插入数据库中（仅有id和email字段，其余为空）
            int teamid = int.Parse(Session["TeamID"].ToString());
            TeamDB TDB = new TeamDB();
            String teamName = TDB.GetTeamByID(teamid).Name;
            int InsertResult = ud.Insert(CurrentEmail, teamid);
            int InvitorID = int.Parse(Session["UserID"].ToString());
            String InvitorName = ud.GetUserByID(InvitorID).Name;
            if (InsertResult == -1)
            {
                //发送邮件给用户，告知被邀请、已自动加入
                String InviteeName = ud.GetUserByEmail(CurrentEmail).Name;
                String MailContent = "尊敬的用户" + InviteeName + "，您好:" + InvitorName;
                MailContent += "在CoCo（在线协作平台）网站上邀请您加入团队" + teamName;
                MailContent += "：“" + WantToSay + "”~~~故系统自动为您发送了这封邮件，通知您已经自动加入该团队。    CoCo:" + Request.Url.Host + "/CoCo3/Index.aspx";
                SendSMTPEMail("smtp.gmail.com", "huangyg11@gmail.com", "zkegtomkpztvnvib", CurrentEmail, "有人在CoCo上邀请您啦！",MailContent);
            }
            else if (InsertResult == 1)
            {
                //发送邮件给用户，告知被邀请，请他注册，注册后将自动加入（给出网站地址）
                String MailContent = "您好:" + InvitorName;
                MailContent += "在CoCo（在线协作平台）网站上邀请您加入团队" + teamName;
                MailContent += "：“" + WantToSay + "”~~~故系统自动为您发送了这封邮件，我们期待您注册成为CoCo的用户。待您注册后，将自动加入该团队。    CoCo:" + Request.Url.Host+"/CoCo3/Index.aspx";
                SendSMTPEMail("smtp.gmail.com", "huangyg11@gmail.com", "zkegtomkpztvnvib", CurrentEmail, "有人在CoCo上邀请您啦", MailContent);
            }
        }

        protected void ButtonDeleteMember_Click(object sender, EventArgs e)
        {
            UserDB ud = new UserDB();
            TeamDB td = new TeamDB();
            RelUserTeamDB RUTDB = new RelUserTeamDB();
            String CurrentName = OldMemberName.Value;
            if (CurrentName == "")
            {
                Response.Write("<script>alert('请先输入ta的用户名吧><')</script>");
                return;
            }
            Team team = td.GetTeamByID(int.Parse(Session["TeamID"].ToString()));
            if (RUTDB.remRel(team, ud.GetUserByName(CurrentName)) == 1)
                Response.Write("<script>alert('已成功删除该用户')</script>");
            else
                Response.Write("<script>alert('该用户本来就不在本团队啦><')</script>");
        }

        public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
        {
            System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);

            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            client.Port = 587;
            client.EnableSsl = true;
            client.Send(message);
        }
    }
}